<?php
namespace Home\Controller;
use Common\Controller\CommonController;

class MessageController extends CommonController {
	public function _initialize(){

		parent::_initialize();
	}
	
	/**
	 * 显示个人信息列表
	 */
	public function index(){
		$member_id=$_SESSION['member_id'];
		$list=M('Message_all')->where('u_id = '.$member_id)->order('add_time desc')->select();
		$data['list']=$list;
		$this->ajaxReturn($data,'JSONP');
	}
	
	/**
	 * 读取消息库中有自己消息的列表并且存储至个人消息库中
	 * @param $id 用户ID
	 * @param $login_time 用户最后一次登录时间
	 * @return bool 返回 成功失败
	 */
	public function pullMessage($id,$login_time){
		if(empty($id)){
			return false;
		}
		if(empty($login_time)){
			return false;
		}
		//消息库
		$M_message_all = M('message_all');
		//用户消息库
		$M_message = M('message');
		$messageAllWhere['add_time'] = array('EGT',$login_time);
		$messageAllWhere['_string'] = " u_id= -1 or  u_id = $id";
		$message_info = $M_message_all->where($messageAllWhere)->select();
		if($message_info){
			foreach ($message_info as $vo) {
				$data[] = array(
						'member_id'=>$id,
						'title'=>$vo['title'],
						'type' => $vo['type'],
						'content'=> $vo['content'],
						'add_time'=> $vo['add_time'],
						'status' => 0,//未读
						'message_all_id'=> $vo['id'],
				);
			}
			if($M_message->addAll($data)===false){
				return false;
			}
		}
		return true;
	}
	
	
}